package com.example.hanchen.yongkanggy.home.base;

import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.fragment.app.FragmentActivity;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProviders;


import com.example.hanchen.yongkanggy.utils.NetWorkHelper;



import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

public abstract class BaseDatabindingActivity<B extends ViewDataBinding, VM extends BaseViewModel> extends AppCompatActivity   {
    protected B mDatabinding;
    protected VM baseViewModel;
    private int viewModelId;
    private NetWorkHelper mNetworkHelper;
    protected long noNetWorkTime;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initParam();
        //私有的初始化Databinding和ViewModel方法
        initViewDataBinding(savedInstanceState);
        initView();
        initData();
        initEvent();
        mNetworkHelper = new NetWorkHelper(this);

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mDatabinding != null) {
            mDatabinding.unbind();
        }

//        ActivityStacks.getInstance().removeActivity(this);

    }

    private void initViewDataBinding(Bundle savedInstanceState) {
        mDatabinding = DataBindingUtil.setContentView(this, initLayoutID(savedInstanceState));
        viewModelId = initVariableId();
        baseViewModel = initViewModel();
        if (baseViewModel == null) {
            Class modelClass;
            Type type = getClass().getGenericSuperclass();
            if (type instanceof ParameterizedType) {
                modelClass = (Class) ((ParameterizedType) type).getActualTypeArguments()[1];
            } else {
                //如果没有指定泛型参数，则默认使用BaseViewModel
                modelClass = BaseViewModel.class;
            }
            baseViewModel = (VM) createViewModel(this, modelClass);
        }
        //让ViewModel拥有View的生命周期感应
        getLifecycle().addObserver(baseViewModel);
        baseViewModel.setContext(this);
        //关联ViewModel
        mDatabinding.setVariable(viewModelId, baseViewModel);
    }


    @Override
    protected void onResume() {
        super.onResume();
        mNetworkHelper.registerNetWorkStateReceiver();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mNetworkHelper.unRegisterNetWorkStateReceiver();
    }



    protected void onNetworkEvent(int netStatus) {
    }

    protected ViewGroup llBadNetStatus() {
        return new FrameLayout(this);
    }

    protected abstract int initLayoutID(Bundle savedInstanceState);

    /**
     * 初始化ViewModel的id
     *
     * @return BR的id
     */
    public abstract int initVariableId();

    protected VM initViewModel() {
        return null;
    }

    //页面接受的参数方法
    public void initParam() {

    }

    protected abstract void initView();

    protected abstract void initData();

    protected void initEvent() {
    }

    /**
     * 创建ViewModel
     *
     * @param cls
     * @param <T>
     * @return
     */
    public <T extends ViewModel> T createViewModel(FragmentActivity activity, Class<T> cls) {
        return ViewModelProviders.of(activity).get(cls);
    }



}
